package com.deparse.dpframe.utils;

import java.io.File;

/**
 * Created by MartinDKent on 2016/1/16.
 */
public class DPLogUtil {
    private static boolean debugEnabled = true;

    private static String defaultTag = "DPFrame";

    public static boolean isDebugEnabled() {
        return debugEnabled;
    }

    public static void setDebugEnabled(boolean enabled) {
        DPLogUtil.debugEnabled = enabled;
    }

    public static String getTag() {
        return defaultTag;
    }

    public static void setTag(String defaultTag) {
        DPLogUtil.defaultTag = defaultTag;
    }

    public static void v(String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.v(getTag(), msg + tmp);
        }
    }

    public static void d(String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.d(getTag(), msg + tmp);
        }
    }

    public static void e(String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.e(getTag(), msg + tmp);
        }
    }

    public static void v(String tag, String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.v(tag, msg + tmp);
        }
    }

    public static void d(String tag, String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.d(tag, msg + tmp);
        }
    }

    public static void e(String tag, String msg) {
        if (isDebugEnabled()) {
            StackTraceElement element = Thread.currentThread().getStackTrace()[3];
            String tmp = "[" + element.getFileName() + "," + element.getClassName().substring(element.getClassName().lastIndexOf(".")) + "," + element.getMethodName() + "," + element.getLineNumber() + "]";
            android.util.Log.e(tag, msg + tmp);
        }
    }

    public static void enter(String msg) {
        printByType(defaultTag, "enter", msg);
    }

    public static void enter(String tag, String msg) {
        printByType(tag, "enter", msg);
    }

    public static void leave(String msg) {
        printByType(defaultTag, "leave", msg);
    }

    public static void leave(String tag, String msg) {
        printByType(tag, "leave", msg);
    }

    public static void printByType(String tag, String type, String msg) {
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        if (isDebugEnabled()) {
            if (null == elements || elements.length < 4) {
                return;
            }

            StackTraceElement e = elements[4];
            String file = e.getFileName();
            String cls = e.getClassName();
            String method = e.getMethodName();
            int line = e.getLineNumber();

            if (file.lastIndexOf(File.separator) >= 0) {
                file = file.substring(file.lastIndexOf(File.separator));
            }
            if (cls.lastIndexOf(".") >= 0) {
                cls = cls.substring(cls.lastIndexOf("."));
            }
            android.util.Log.d(tag, "[" + file + "]" + cls + "." + method + "(" + msg + ")" + type + " at line:" + line);
        }
    }

    public static void printStackTrace(String... flag) {
        String flg = defaultTag;
        if (null != flag && 1 == flag.length && null != flag[0]) {
            flg = flag[0];
        }
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        for (int i = 0; i < elements.length; i++) {
            android.util.Log.d(flg, " FILE:" + elements[i].getFileName() + ", CLASS:" + elements[i].getClassName() + ", METHOD:" + elements[i].getMethodName() + ", LINE" + elements[i].getLineNumber());
        }
    }
}
